﻿using System.Configuration;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;

namespace Cognifide.PowerShell.Shell.Commands.Analytics
{
    public class AnalyticsBaseCommand : BaseCommand
    {
        private static analytics context;

        protected static analytics Context
        {
            get
            {
                if (context == null)
                {
                    MetadataWorkspace workspace = new MetadataWorkspace(
                        new string[] { "res://*/" }, new Assembly[] { Assembly.GetExecutingAssembly() });
                    string connString = ConfigurationManager.ConnectionStrings["analytics"].ConnectionString;
                    SqlConnection sqlConnection = new SqlConnection(connString);
                    EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection);
                    context = new analytics(entityConnection);
                }
                return context;
            }
        }

        protected virtual void PipeQuery<T>(IQueryable<T> query)
        {
            foreach (var result in query)
            {
                WriteObject(result);
            }
        }

    }
}
